package ne.analyzer.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ne.analyzer.entities.Reference;
import ne.analyzer.importer.ImporterInf;

/**
 * 题录导入功能
 * 
 * @author sunny
 *
 */
public class ImportAction {
	private ImporterInf inf;
	private List<Map<String, Object>> references;

	public ImportAction(ImporterInf inf) {
		this.inf = inf;
		references = new ArrayList<Map<String, Object>>();
	}

	public void extract(String content) {
		references.clear();
		List<String> refStrs = inf.referencesSplit(content);
		for (String refStr : refStrs) {
			Map<String, Object> fields = new HashMap<String, Object>();
			inf.fieldSplit(refStr, fields);
			if (!fields.isEmpty()) {
				references.add(fields);
			}
		}
	}

	public List<Reference> convert() {
		List<Reference> result = new ArrayList<Reference>();
		if (!references.isEmpty()) {
			for (Map<String, Object> fields : references) {
				Reference ref = inf.convert(fields);
				result.add(ref);
			}
		}
		return result;
	}
}
